{% extends "templ.html" %}

{% block body%}
<form id='chirping_form' role="form" action="/chirping/upt" method="post">
   <div class="form-group">
      <label for="name">Text</label>
      <input id='timestamp' name='timestamp' type='text' value='{{ timestamp }}' hidden>
      <textarea id='text' name="text" class="form-control" rows="3"></textarea>
      <button id='submit' type="button" class="btn btn-default">Submit</button>
   </div>
</form>
<script>
   function submit(){
      //$('input#timestamp').val(new Date().getTime())
      $.ajax({
         type: "POST",
         url: "/chirping/upt",
         data: $('#chirping_form').serialize(),
         success: function (result) {
            window.location = '/chirpings'
         },
         error : function() {
         }
      });
   }
function fetch(){
   timestamp = $('input#timestamp').val()
   $.ajax({
      type: "GET",
      dataType: 'json',
      url: "/chirping/fetch?timestamp=" + timestamp,
      success: function (result) {
         //window.location = '/chirpings'
         $('textarea#text').val(result[0].fields.text)
      },
      error : function() {
      }
   });
}
window.onload = function(){
   $('button#submit').on('click', function(){
      submit();
   });
   fetch();
}

</script>
{% endblock %}

